package com.example.shopdemo.controller.dashboard;

import com.example.shopdemo.dto.ShopExecution;
import com.example.shopdemo.entity.Area;
import com.example.shopdemo.entity.Shop;
import com.example.shopdemo.entity.ShopCategory;
import com.example.shopdemo.service.AreaServiceImpl;
import com.example.shopdemo.service.ShopCategoryServiceImpl;
import com.example.shopdemo.service.ShopServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-26
 **/
@Controller
@RequestMapping("/dashboard")
public class ShopListController {


    @Autowired
    private ShopCategoryServiceImpl shopCategoryService;

    @Autowired
    private AreaServiceImpl areaService;

    @Autowired
    private ShopServiceImpl shopService;

    @RequestMapping("/listShopsPageInfo")
    @ResponseBody
    public Map<String,Object> listShopPageInfo(@RequestParam(value = "parentId",required = false)Long parentId){
        Map<String,Object> modelMap = new HashMap<>();
        List<ShopCategory> shopCategoryList = null;
        if(parentId!=null){
            //如果parentId存在，则取出该一级ShopCategory下的二级ShopCategory列表
            ShopCategory shopCategoryCondition = new ShopCategory();
            shopCategoryCondition.setParentId(parentId);
            shopCategoryList = shopCategoryService.queryShopCategory(shopCategoryCondition);
        }else{
            //如果parentId不存在，则取出所以一级ShopCategory(用户在首页选择的是全部商店列表)
            shopCategoryList = shopCategoryService.queryFirstLevelShopCategory();
        }
        if(shopCategoryList==null){
            modelMap.put("success",false);
            modelMap.put("errMsg","shopCategoryList is null");
            return modelMap;
        }
        modelMap.put("shopCategoryList",shopCategoryList);
        List<Area> areaList  = areaService.getAreaList();
        if(areaList==null){
            modelMap.put("success",false);
            modelMap.put("errMsg","areaList is null");
            return modelMap;
        }
        modelMap.put("areaList",areaList);
        modelMap.put("success",true);
        return modelMap;
    }


    /**
     * 获取指定查询条件下的店铺列表
     * @param
     * @return
     */
    @RequestMapping("/listShops")
    @ResponseBody
    public Map<String,Object> listShops(@RequestParam("page")int page,
                                        @RequestParam("size")int size,@RequestParam(value = "parentId",required = false)Long parentId,
                                        @RequestParam(value = "areaId",required = false)Integer areaId,@RequestParam(value = "shopName",required = false)String shopName,
                                        @RequestParam(value = "shopCategoryId",required = false)Long shopCategoryId){
        Map<String,Object> modelMap = new HashMap<>();
        Shop shopCondition = new Shop();
        if(parentId!=null){
            shopCondition.setParentCategoryId(parentId);
        }
        if(areaId!=null){
            shopCondition.setAreaId(areaId);
        }
        if(shopCategoryId!=null){
            shopCondition.setShopCategoryId(shopCategoryId);
        }
        if(shopName!=null){
            shopCondition.setShopName(shopName);
        }
        shopCondition.setEnableStatus(1);
        ShopExecution shopExecution = shopService.getShopList(shopCondition,page,size);
        if(shopExecution.getShopList()!=null) {
            modelMap.put("shopList", shopExecution.getShopList());
            modelMap.put("count", shopExecution.getCount());
            modelMap.put("success", true);
        }else{
            modelMap.put("success",false);
            modelMap.put("errMsg",shopExecution.getStateInfo());
        }

        return modelMap;
    }
}
